#encoding: utf-8

from datakit_framework import DataKitFramework
import os

class Myscript(DataKitFramework):
    name = 'Myscript'
    interval = 10 # triggered interval seconds.

    # if your datakit ip is 127.0.0.1 and port is 9529, you won't need use this,
    # just comment it.
    def __init__(self, **kwargs):
        super().__init__(ip = os.getenv('DATAKIT_HOST'), port = os.getenv('DATAKIT_PORT'))

    def run(self):
        # Test 1: Custom report(metrics).
        measurement = "measurement1"
        data = [
        {
            "measurement": measurement, # 指标名称。
            "tags": {
                "tag_name": "tag_value", # 自定义 tag，根据自己想要标记的填写
            },
            "fields": {
                "count": 1, # 指标
            },
        },
        ]

        in_data = {
            'M': data,
            'input': "py_from_docker"
        }

        self.report(in_data) # you must call self.report here

        # Test 2: Feed metrics example.
        measurement = "measurement2"
        tags = {"tag1": "val1", "tag2": "val2"}
        fields = {"custom_field1": "val1","custom_field2": 1000}
        kwargs = {"custom_key1":"custom_value1", "custom_key2": "custom_value2", "custom_key3": "custom_value3"}

        self.feed_metric(
            measurement=measurement,
            tags=tags,
            fields=fields,
            **kwargs
            )

        # Test 3: Feed logging example.
        measurement = "measurement3"
        message = "This is the message for testing"
        self.feed_logging(
            source=measurement,
            tags=tags,
            message=message,
            **kwargs
            )

        # Test 4: Feed object example.
        measurement = "measurement4"
        name = "name"
        self.feed_object(
            cls=measurement,
            name=name,
            tags=tags,
            fields=fields,
            **kwargs
            )

        # Test 5: Feed df_source=user event.
        tags = {"tag1": "val1", "tag2": "val2"}
        date_range = 10
        status = 'info'
        event_id = 'event_id'
        title = 'title'
        message = 'message'
        kwargs = {"custom_key1":"custom_value1", "custom_key2": "custom_value2", "custom_key3": "custom_value3"}

        user_id="user_id"
        self.feed_user_event(
            user_id,
            tags, date_range, status, event_id, title, message, **kwargs
            )

        # Test 6: Feed df_source=monitor event.
        dimension_tags='{"host":"web01"}' # dimension_tags must be the String(JSON format).
        self.feed_monitor_event(
            dimension_tags,
            tags, date_range, status, event_id, title, message, **kwargs
            )

        # Test 7: Feed df_source=system event.
        title = 'feed_system_event'
        self.feed_system_event(
            tags, date_range, status, event_id, title, message, **kwargs
            )
        
        os._exit(0)
